package net.maku.events.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import lombok.AllArgsConstructor;
import net.maku.events.vo.ActivityBaseJoinVO;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.events.convert.UserJoinActivityConvert;
import net.maku.events.entity.UserJoinActivityEntity;
import net.maku.events.query.UserJoinActivityQuery;
import net.maku.events.vo.UserJoinActivityVO;
import net.maku.events.dao.UserJoinActivityDao;
import net.maku.events.service.UserJoinActivityService;
import net.maku.framework.security.user.SecurityUser;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户参与的活动
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@Service
@AllArgsConstructor
public class UserJoinActivityServiceImpl extends BaseServiceImpl<UserJoinActivityDao, UserJoinActivityEntity> implements UserJoinActivityService {

    @Override
    public PageResult<UserJoinActivityVO> page(UserJoinActivityQuery query) {
        IPage<UserJoinActivityEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        List<UserJoinActivityVO> result = BeanUtil.copyToList(page.getRecords(), UserJoinActivityVO.class);
        return new PageResult<>(result, page.getTotal());
    }

    @Override
    public boolean checkJoin(Integer businessType, Long businessId) {
        UserJoinActivityEntity entity = this.getOne(new LambdaQueryWrapper<UserJoinActivityEntity>().eq(UserJoinActivityEntity::getBusinessType, businessType)
                .eq(UserJoinActivityEntity::getActivityId, businessId)
                .eq(UserJoinActivityEntity::getUserId, SecurityUser.getUserId()));
        return ObjectUtils.isNotEmpty(entity);
    }

    private LambdaQueryWrapper<UserJoinActivityEntity> getWrapper(UserJoinActivityQuery query) {
        LambdaQueryWrapper<UserJoinActivityEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserJoinActivityEntity::getUserId, SecurityUser.getUserId()).eq(UserJoinActivityEntity::getStatus, 1).orderByDesc(UserJoinActivityEntity::getId);
        return wrapper;
    }

    @Override
    public void save(Integer businessType, Long businessId, String activityTitle, String thumbnail, Date startTime, Date endTime) {
        UserJoinActivityEntity entity = new UserJoinActivityEntity();
        entity.setActivityId(businessId);
        entity.setBusinessType(businessType);
        entity.setUserId(SecurityUser.getUserId());
        entity.setActivityTitle(activityTitle);
        entity.setThumbnail(thumbnail);
        entity.setActivityStartTime(startTime);
        entity.setActivityEndTime(endTime);
        entity.setStatus(1);
        baseMapper.insert(entity);
    }

    @Override
    public void getJoinActivityIdList(Integer businessType, List<? extends ActivityBaseJoinVO> activityBaseJoinVOS) {
        if (ObjectUtils.isEmpty(SecurityUser.getUser())) {
            return;
        }
        List<UserJoinActivityEntity> entities = baseMapper.selectList(Wrappers.<UserJoinActivityEntity>lambdaQuery().eq(UserJoinActivityEntity::getBusinessType, businessType)
                .eq(UserJoinActivityEntity::getUserId, SecurityUser.getUserId()));
        if (CollectionUtils.isNotEmpty(entities)) {
            List<Long> joinActivityIdList = entities.stream().map(UserJoinActivityEntity::getActivityId).collect(Collectors.toList());
            activityBaseJoinVOS.forEach(item -> {
                if (joinActivityIdList.contains(item.getId())) {
                    item.setJoinFlag(true);
                }
            });
        }
    }


}